2 Variable Plotting with ggplot2

In this lecture we will briefly show some examples of how you can compare two variables from a dataset. For these examples, you'll need the full control of ggplot instead of using qplot(), but we will show a quick example of what qplot is capable of. We'll use the built-in ggplot2 movies dataset:

In [ ]:
library(ggplot2)
df <- movies

qplot()

In [28]:
qplot(x=year, y=rating, data = df, geom = "density2d")
In [ ]:
In [6]:
pl <- ggplot(movies,aes(x = year,y=rating))

2d Bin Chart

In [7]:
pl + geom_bin2d()
In [13]:
# Control bin sizes
pl + geom_bin2d(binwidth=c(2,1))

2d Density Plot

In [29]:
pl + geom_density2d()

2d Hex Plot

In [30]:
pl + geom_hex()
In [32]:
pl + geom_hex() + scale_fill_gradient(high='red',low='blue')

Alright that's it for the basics of plotting 2 variables against each other!